package com.cysyz.web.modules.base.support.controller;

import com.cysyz.web.modules.base.support.ServletActionContext;
import com.cysyz.web.modules.base.support.SysConstants;
import com.cysyz.web.modules.base.user.domain.SysUser;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by cuiyong on 2014/12/15.
 */
@Controller
public abstract class SpringMVCBaseController extends BaseController{

    @ModelAttribute
    public void populateModule() {
        setRequestAttribute("controllerUrlPrefix", getRequest().getContextPath() + this.getClass().getAnnotation(RequestMapping.class).value()[0]);
        setRequestAttribute("current", getRequest().getContextPath() + this.getClass().getAnnotation(RequestMapping.class).value()[0]);

    }

    /** binder用于bean属性的设置 */
    @InitBinder
    public void initBinder(WebDataBinder binder){
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @Override
    public HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }

    @Override
    public HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }


}
